import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# 读取CSV文件数据
data = pd.read_csv('600887历史数据.csv', encoding='gbk')

# 提取收盘价数据
historical_prices = data['收盘价'].values

# 计算日收益率
daily_returns = np.diff(historical_prices) / historical_prices[:-1]

# 计算平均收益率和标准差
mean_return = np.mean(daily_returns)
std_dev_returns = np.std(daily_returns)

# 模拟未来250个交易日的股价
future_days = 250
simulated_prices_list = []

for _ in range(20):  # 进行20次模拟
    simulated_prices = np.zeros(future_days)
    simulated_prices[0] = historical_prices[-1]  # 从最后一个历史价格开始
    for i in range(1, future_days):
        return_for_day = np.random.normal(mean_return, std_dev_returns)
        simulated_prices[i] = simulated_prices[i-1] * (1 + return_for_day)
    simulated_prices_list.append(simulated_prices)

# 计算平均模拟价格
average_simulated_price = np.mean(simulated_prices_list, axis=0)

# 计算所有模拟价格的标准差（用于置信区间）
std_dev_simulated_prices = np.std(simulated_prices_list, axis=0)

# 绘制实际股价和预测股价
plt.figure(figsize=(12, 6))
plt.plot(np.arange(len(historical_prices)), historical_prices, label='Actual Price', color='blue')

# 绘制平均预测股价路径
plt.plot(np.arange(future_days) + len(historical_prices), average_simulated_price, label='Average Simulated Price', color='red')

# 绘制所有模拟的股价路径
for prices in simulated_prices_list:
    plt.plot(np.arange(future_days) + len(historical_prices), prices, color='gray', alpha=0.1)

# 绘制置信区间
plt.fill_between(np.arange(future_days) + len(historical_prices), 
                 average_simulated_price - 1.96 * std_dev_simulated_prices, 
                 average_simulated_price + 1.96 * std_dev_simulated_prices, color='gray', alpha=0.5)

plt.title('伊利股票价格预测')
plt.xlabel('Days')
plt.ylabel('Price')
plt.legend()
plt.grid(True)
plt.show()